package com.why.product.web;

import com.why.product.entity.CategoryEntity;
import com.why.product.service.CategoryService;
import com.why.product.vo.CateJsonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @program: why-gulimall
 * @description:
 * @author: @why
 * @create: 2021-05-21 21:54
 **/
@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;






//    thymeleaf中配置了前缀和后缀 因此不用在后面写html
    @GetMapping({"/","/index.html"})
    public String toIndex(Model model){


        // TODO: 2021/5/21 查出所有的一级分类
        //进行了拼串 classpath:/template/+ 返回值 .html

       List<CategoryEntity> categoryEntities=  categoryService.getLeveloneCategories();
        model.addAttribute("categories",categoryEntities);
        return "index";
    }

    //index/json/catalog.json
    @GetMapping(value = {"/index/json/catalog.json"})
    @ResponseBody
    public Map<String, List<CateJsonVo>> getCatalogJson() {

        Map<String, List<CateJsonVo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;

    }


}
